/**
 * 
 * @author Diego Galaz
 */

package eft.android.view;

import me.croute.calendarexample.R;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

//import eft.android.R;

public class ExcerciseHistory extends Activity {
	
	TableLayout exercises_table;
	
	// This array is used only for demonstration purposes
	public static String[] girls = { "zzz", "Fran", "Diane", "Elizabeth",
		"Angie", "Barbara", "Chelsea", "Cindy", "Grace", "Helen",
		"Isabel", "Jackie", "Karen", "Linda", "Mary", "Nancy", "a",
		"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"};
	
	// This array is used only for demonstration purposes
	public static String[] girlsSorted = { "Angie", "Barbara", "Chelsea", 
		"Cindy", "Diane", "Elizabeth", "Fran", "Grace", "Helen", 
		"Isabel", "Jackie", "Karen", "Linda", "Mary", "Nancy"  };
	
	// This array is used only for demonstration purposes
	public static String[] nums = { "2012/01/05", "2012/01/07", "2012/01/10", 
		"2012/01/20", "2012/01/28", "2012/02/04", "2012/02/10", "2012/02/11", 
		"2012/01/20", "2012/01/28", "2012/02/04", "2012/02/10", "2012/02/11",
		"2012/01/20", "2012/01/28" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.exercisehistory);
            
            exercises_table = (TableLayout) findViewById(R.id.exercises_table);
            
            Button date = (Button) findViewById(R.id.date_sort_button);
            Button alpha = (Button) findViewById(R.id.alpha_sort_button);
            
            date.setOnClickListener(onDateButton);
            alpha.setOnClickListener(onAlphaButton);
            
            fillExerciseTable();
    	}
    	catch (Exception e) {
			Log.e("ERROR", "ERROR IN CODE: " + e.toString());
			e.printStackTrace();
		}

    }

    /** Create click listeners for buttons */
    private View.OnClickListener onDateButton = new View.OnClickListener() {
		
		public void onClick(View v) {
			fillExerciseTableSortedByDate();			
		}
	};
    
	private View.OnClickListener onAlphaButton = new View.OnClickListener() {
		
		public void onClick(View v) {
			fillExerciseTableSortedByAlpha();
		}
	};
    
    /**
     * Dynamically creates a table listing the exercise history of the user.
     * @param exerciseList ordered list of exercises with their respective time and 
     * frequency.
     */
    void fillExerciseTable() {
    	
    	TableRow row;
    	TextView t1, t2;
    	
    	// Fill out the exercise history table
    	for (int current = 0; current < girls.length; current++ ) {
    		row = new TableRow(this);
    		
    		t1 = new TextView(this);
    		t2 = new TextView(this);
    		
    		t1.setText(girls[current]);
    		t2.setText(girls[current]);
    		
    		row.addView(t1);
    		row.addView(t2);

    		exercises_table.addView(row, new TableLayout.LayoutParams(
    				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    	}
    }
    
    void fillExerciseTableSortedByDate() {
    	TableRow row;
    	TextView t1, t2;

    	// Clear the table
		exercises_table.removeAllViewsInLayout();
    	
    	// Fill out the exercise history table
    	for (int current = 0; current < girlsSorted.length; current++ ) {
    		row = new TableRow(this);
    		
    		t1 = new TextView(this);
    		t2 = new TextView(this);
    		
    		t1.setText(girlsSorted[current]);
    		t2.setText(nums[current]);
    		
    		row.addView(t1);
    		row.addView(t2);
    		
    		exercises_table.addView(row, new TableLayout.LayoutParams(
    				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    	}
    }
    
    void fillExerciseTableSortedByAlpha() {
    	
    	TableRow row;
    	TextView t1, t2;
    	
    	// Clear the table
		exercises_table.removeAllViewsInLayout();
    	
    	// Fill out the exercise history table
    	for (int current = 0; current < girlsSorted.length; current++ ) {
    		row = new TableRow(this);
    		
    		t1 = new TextView(this);
    		t2 = new TextView(this);
    		
    		t1.setText(girlsSorted[current]);
    		t2.setText(girlsSorted[current]);
    		
    		row.addView(t1);
    		row.addView(t2);

    		exercises_table.addView(row, new TableLayout.LayoutParams(
    				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    	}
    }
}